tidyverse 與 gapminderlibrary(tidyverse)
library(gapminder)ggplot() + geom_point() 繪製散佈圖gapminder_2007 <- gapminder %>%
filter(year == 2007)
scatter <- ggplot(gapminder_2007, aes(x = gdpPercap, y = lifeExp)) +
geom_point()scatteraes() 中加入 color =scatter <- ggplot(gapminder_2007, aes(x = gdpPercap, y = lifeExp, color = continent)) +
geom_point()scatterscatter <- ggplot(gapminder_2007, aes(x = gdpPercap, y = lifeExp, color = continent)) +
geom_point() +
scale_x_log10()scatterggplot() + geom_line() 繪製線圖gapminder_tw <- gapminder %>%
filter(country == "Taiwan")
line <- ggplot(gapminder_tw, aes(x = year, y = lifeExp)) +
geom_line()linegapminder_na <- gapminder %>%
filter(country %in% c("China", "Hong Kong, China", "Japan", "Korea, Rep.", "Taiwan"))
multi_lines <- ggplot(gapminder_na, aes(x = year, y = lifeExp, color = country)) +
geom_line()multi_linesggplot() + geom_histogram() 繪製直方圖hist <- ggplot(gapminder_2007, aes(x = lifeExp)) +
geom_histogram(bins = 40)histggplot() + geom_boxplot() 繪製盒鬚圖box <- ggplot(gapminder_2007, aes(x = continent, y = lifeExp)) +
geom_boxplot()boxfacet_wrap()multi_hists <- ggplot(gapminder_2007, aes(x = lifeExp, fill = continent)) +
geom_histogram(bins = 20) +
facet_wrap(~continent, nrow = 2)multi_histsggplot() + geom_bar(stat = "identity")gapminder_2007_na <- gapminder_2007 %>%
filter(country %in% c("China", "Hong Kong, China", "Japan", "Korea, Rep.", "Taiwan"))
barv <- ggplot(gapminder_2007_na, aes(x = country, y = gdpPercap)) +
geom_bar(stat = "identity")barv+ coord_flip()barh <- ggplot(gapminder_2007_na, aes(x = country, y = gdpPercap)) +
geom_bar(stat = "identity")+
coord_flip()barhgridExtra 套件來幫忙grid.arrange() 函數install.packages("gridExtra")
library(gridExtra)
gg1 <- ggplot(gapminder_2007_na, aes(x = country, y = gdpPercap)) +
geom_bar(stat = "identity")
gg2 <- ggplot(gapminder_2007_na, aes(x = country, y = gdpPercap)) +
geom_bar(stat = "identity")+
coord_flip()grid.arrange(gg1, gg2, nrow = 2)
ggplotly() 加入互動性plotly 套件的 ggplotly() 函數install.packages("plotly")
library(plotly)
static_gg <- ggplot(gapminder_2007, aes(x = gdpPercap, y = lifeExp, color = continent)) +
geom_point() +
scale_x_log10()ggplotly(static_gg)R 語言動態視覺化的 Hello World - 利用 RSelenium、plotly 與 shiny 模仿 Hans Rosling 的視覺化大作